package model;

import java.util.Date;

public class ModelBase {
	protected Long id;
	protected Date lastModifiedTimeStamp;

	/**
	 * @return
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @return
	 */
	public Date getLastModifiedTimeStamp() {
		return lastModifiedTimeStamp;
	}

	public boolean isNew() {
		return id == null ? true : false;
	}

	/**
	 * @param id
	 * @return
	 */
	public ModelBase setId(Long id) {
		if (id == null) {
			throw new IllegalArgumentException("ID must not be null");
		}
		if (this.id != null) {
			throw new IllegalArgumentException("ID must not be changed!");
		}
		this.id = id;
		return this;
	}

	/**
	 * @param timeStamp
	 * @return
	 */
	public ModelBase setLastModifiedTimeStamp(Date timeStamp) {
		if (timeStamp == null || (timeStamp.compareTo(new Date())) > 0) {
			throw new IllegalArgumentException(
					"Timestamp must not be null or later than now!");
		}
		this.lastModifiedTimeStamp = timeStamp;
		return this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime
				* result
				+ ((lastModifiedTimeStamp == null) ? 0 : lastModifiedTimeStamp
						.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ModelBase other = (ModelBase) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (lastModifiedTimeStamp == null) {
			if (other.lastModifiedTimeStamp != null)
				return false;
		} else if (!lastModifiedTimeStamp.equals(other.lastModifiedTimeStamp))
			return false;
		return true;
	}

	public int compareTo(Datentraeger other) {

		return 0;
	}

}
